package br.inf.solutions.apresentacoes.ssp.control;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BaseItensBulletinController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.BehaviorChange;

@Service
@Scope("prototype")
public class BehaviorChangeController extends BaseItensBulletinController<BehaviorChange> {

	public BehaviorChangeController() {
		super(BehaviorChange.class);
	}
	
	@Override
	public Return save() {
		Return ret = new Return(true);
		BehaviorChange behavior = getEntity();
		
		behavior.setBehaviorOld(behavior.getPerson().getBehavior());
		
		ret.concat(super.save());
		
		return ret;
	}
	
	public Return setNewBehavior(){
		if(!(new Date().before(getEntity().getDateInitial()))){
			getEntity().getPerson().setBehavior(getEntity().getBehaviorCurrent());
		}
		return new Return(true);
	}

	public Return getNotPublicatedBehaviorChange(){
		data.put("complement", "AND publication = " + getDefaultPublication().getId());
		
		return searchByHQL();
	}

	@Override
	protected String constructTextItem(Object modifer) {
		BehaviorChange b = getEntity();
		Person p = getEntity().getPerson();
		String s;
		s = "O " + p.getPost().getName().toUpperCase() + " " + p.getSquare().getAcronym() + " RG " + p.getRg() + " " + p.getFullName().toUpperCase() + ", modificou seu comportamento"
				+ " para " + b.getBehaviorCurrent().toString().toUpperCase() + " a contar de " + new SimpleDateFormat("dd/MM/yyyy").format(b.getDateInitial()) + ".";
		if(b.getReasonBehavior()!=null){
			s+= " Pelo seguinte motivo: " + b.getReasonBehavior();
		}
		return s;
	}
	
	@Override
		public Return afterPublicate() {
			Return ret = super.afterPublicate();
			ret.concat(setNewBehavior());
			return ret;
		}
	
}
